﻿using XYS.Service.Core.Models.APILoggerModels;

namespace XYS.Core.Models;

/// <summary>
/// API请求日志内容
/// </summary>
public class APILoggerInfo : CurrentUserInfo, IApiPathField, IResponseBodyField
{
    /// <summary>
    /// 请求id（每次请求都会生成一个uuid）
    /// </summary>
    public Guid Id { get; set; }

    /// <summary>
    /// 当前服务名称
    /// </summary>
    public string ServiceName => GlobalCoreSetting.ServiceName;

    /// <summary>
    /// 请求方法类型
    /// GET\POST\PUT\DELETE
    /// </summary>
    public string Method { get; set; }

    /// <summary>
    /// 接口host
    /// </summary>
    public string ApiHost { get; set; }

    /// <summary>
    /// 接口端口
    /// </summary>
    public int? ApiPort { get; set; }

    /// <summary>
    /// 接口path
    /// </summary>
    public string ApiPath { get; set; }

    /// <summary>
    /// URL请求参数
    /// </summary>
    public string QueryString { get; set; }

    /// <summary>
    /// 接口请求地址
    /// </summary>
    public string APIUrl { get; set; }
    /// <summary>
    /// 请求头
    /// </summary>
    public List<KeyValueDTO<string, string>> RequestHeaders { get; set; } = new();

    /// <summary>
    /// 项目运行环境
    /// </summary>
    public string EnumEnvironmentType { get; set; }

    /// <summary>
    /// 额外的用户数据（json）
    /// </summary>
    public string UserExJson { get; set; }

    /// <summary>
    /// ContentType
    /// </summary>
    public string ContentType { get; set; }

    /// <summary>
    /// 请求携带的 body数据
    /// </summary>
    public string RequestBody { get; set; }

    /// <summary>
    /// 状态码
    /// </summary>
    public EnumStatusCode StatusCode { get; set; } = EnumStatusCode.OK;

    /// <summary>
    /// 状态消息（状态描述，状态为2开头以外时，应当给这个字段填充原因）
    /// </summary>
    public string StatusMessage { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    public string Remark { get; set; }

    /// <summary>
    /// 响应数据
    /// </summary>
    public string ResponseBody { get; set; }

    /// <summary>
    /// 客户端Ip
    /// </summary>
    public string RealIpAddress { get; set; }

    /// <summary>
    /// 转发Ip
    /// </summary>
    public string ForwardedIpAddress { get; set; }

    /// <summary>
    /// 请求开始时间
    /// </summary>
    public DateTime BeginTime { get; set; }

    /// <summary>
    /// 请求结束时间
    /// </summary>
    public DateTime EndTime { get; set; }

    /// <summary>
    /// 耗时
    /// </summary>
    public string ElapsedTime => (EndTime - BeginTime).ToString();
}

/// <summary>
/// http请求日志内容
/// </summary>
public class HttpLoggerInfo
{
    /// <summary>
    /// 请求id
    /// </summary>
    public Guid Id { get; set; }

    /// <summary>
    /// 当前服务名称
    /// </summary>
    public string ServiceName => GlobalCoreSetting.ServiceName;

    /// <summary>
    /// 请求方法类型
    /// GET\POST\PUT\DELETE
    /// </summary>
    public string Method { get; set; }

    /// <summary>
    /// 接口host
    /// </summary>
    public string ApiHost { get; set; }

    /// <summary>
    /// 接口端口
    /// </summary>
    public int? ApiPort { get; set; }

    /// <summary>
    /// 接口path
    /// </summary>
    public string ApiPath { get; set; }

    /// <summary>
    /// URL请求参数
    /// </summary>
    public string QueryString { get; set; }

    /// <summary>
    /// 接口请求地址
    /// </summary>
    public string APIUrl { get; set; }

    /// <summary>
    /// 项目运行环境
    /// </summary>
    public string EnumEnvironmentType { get; set; }

    /// <summary>
    /// 请求携带的 body数据
    /// </summary>
    public string RequestBody { get; set; }


    /// <summary>
    /// 状态码
    /// </summary>
    public EnumStatusCode StatusCode { get; set; } = EnumStatusCode.OK;

    /// <summary>
    /// 状态消息（状态描述，状态为2开头以外时，应当给这个字段填充原因）
    /// </summary>
    public string StatusMessage { get; set; }

    /// <summary>
    /// 请求头
    /// </summary>
    public List<KeyValueDTO<string, string>> RequestHeaders { get; set; } = new();

    /// <summary>
    /// 备注
    /// </summary>
    public string Remark { get; set; }

    /// <summary>
    /// 响应数据
    /// </summary>
    public string ResponseBody { get; set; }

    /// <summary>
    /// 响应数据-map
    /// </summary>
    public List<KeyValueDTO<string, string>> ResponseBodyMap { get; set; } = new();

    /// <summary>
    /// 响应头
    /// </summary>
    public List<KeyValueDTO<string, string>> ResposeHeaders { get; set; } = new();
    /// <summary>
    /// 请求开始时间
    /// </summary>
    public DateTime BeginTime { get; set; }

    /// <summary>
    /// 请求结束时间
    /// </summary>
    public DateTime EndTime { get; set; }

    /// <summary>
    /// 耗时
    /// </summary>
    public string ElapsedTime => (EndTime - BeginTime).ToString();

}
